package com.cdh.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdh.oa.entity.Menu;
import com.cdh.oa.mapper.MenuMapper;
import com.cdh.oa.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 菜单表 服务实现类
 * </p>
 *
 * @author CDHong
 * @since 2018-11-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> navList() {
        List<Menu> menus = menuMapper.selectList(null);
        //父菜单
        List<Menu> rootMenu = menus.stream().filter(menu -> menu.getParentId() == 0)
                .map(menu -> !menu.getHref().equals("") ?menu.setHref(menu.getHref()+"/"+menu.getId()):menu)
                .collect(Collectors.toList());

        //父菜单映射，组装需要的JSON格式
        return rootMenu.stream().map(root ->
                root.setChildren(menus.stream().filter(menu -> menu.getParentId().equals(root.getId()))
                .map(menu -> menu.setHref(menu.getHref() + "/" + menu.getId())) //菜单地址栏传递当前对应的模块值
                .collect(Collectors.toList()))).collect(Collectors.toList());
    }
}
